package com.ysh.smartplatform.service.impl;

import com.ysh.smartplatform.cache.RoleCache;
import com.ysh.smartplatform.constans.ResultCode;
import com.ysh.smartplatform.event.RoleChangeEvent;
import com.ysh.smartplatform.event.SceneEvent;
import com.ysh.smartplatform.exceptions.AddDataException;
import com.ysh.smartplatform.mapper.RoleMapper;
import com.ysh.smartplatform.mapper.UserRoleMapper;
import com.ysh.smartplatform.pojo.BaseUser;
import com.ysh.smartplatform.pojo.Role;
import com.ysh.smartplatform.service.RoleService;
import com.ysh.smartplatform.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 袁胜虎 on 2022/9/16 19:59
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private RoleMapper roleMapper;

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    private UserRoleMapper userRoleMapper;

    @Autowired
    public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    private RoleCache roleCache;

    @Autowired
    public void setRoleCache(RoleCache roleCache) {
        this.roleCache = roleCache;
    }

    @Override
    public List<Role> findAllRoles() {
        //当前操作需要登陆,为了避免前面controller被绕过,简单判断下,但是此操作实际上应该是判断权限,并且很多操作需要进行此判断,可以通过aop进行操作
        BaseUser user = SecurityUtils.getLoginUser();
        Assert.notNull(user, () -> {
            throw new AddDataException("未登录", ResultCode.PARAM_CONTENT_EMPTY);
        });
        // List<Role> roleList = roleMapper.selectByExample(new RoleExample());
        List<Role> roleList = roleCache.getAllData();
        return roleList;
    }

    @Override
    public List<Role> findRolesByUserId(Long uid) {
        Assert.notNull(uid, () -> {
            throw new AddDataException("参数为空",ResultCode.PARAM_CONTENT_EMPTY);
        });
        //先查询用户的角色
        List<Long> roleIds = userRoleMapper.selectRoleIdByUserId(uid);
        //根据角色id查询数据
        List<Role> roleList = roleCache.getAllData();
        //筛选在对应id范围的数据
        List<Role> result = roleList.stream().filter(role -> roleIds.contains(role.getRoleId())).collect(Collectors.toList());

        return result;
    }

    //根据删除
    @Override
    public void DeleteById(Long Id) {
        roleMapper.DeleteById(Id);
        context.publishEvent(new RoleChangeEvent());
    }

    @Override
    public Role selectByName(String name) {
        Role role = roleMapper.selectByName(name);
        return role;
    }

    @Override
    public void  insertRole(Role role) {
        roleMapper.insertRole(role);
    }

    @Override
    public void updateRole(Role role) {
      roleMapper.updateRole(role);
    }
}
